package com.example.lancooznbk.utils;

/**
 * Created by tangminglong on 16/11/19.
 */

public class TagHelperUtils {

    public static String replaceStartAndEndTag(String str,String tag, String replacement){
        if (str == null) return "";
        String regexP = "(?i)(^(<P>|</P>|<BR>|</BR>)*)|((<P>|</P>|<BR>|</BR>)*$)";//替换首尾所有的<P>标签
        return str.replaceAll(regexP,replacement);
    }

    public static String replacAllTag(String str,String tag, String replacement){
        if (str == null) return "";
        String regexP = "(?i)("+tag+")*";//替换首尾所有的<P>标签
        return str.replaceAll(regexP,replacement);
    }

    /**
     * 修复内容为空但是有下划线，下划线无法画出的bug
     * @param str
     * @return
     */
    public static String replaceNULL_U_Tag(String str){
        if (str == null) return "";
        String regexP = "(?i)<U>(\\s)*((&nbsp;)+)(\\s)*</U>";//
        return str.replaceAll(regexP,"______");
    }

    public static String replaceImageTag(String str){
        if (str == null) return "";
        String regexImg = "(?i)<IMG.*?>";
        return str.replaceAll(regexImg,"");
    }

    public static String replaceTableTag(String str){
        String regexTable = "(?i)<table[^>]*>|</table>";
        return str.replaceAll(regexTable,"");
    }
    /**
     * 排除如果出现"^$)^$)"这类不能分割的情况
     * @param str
     * @param split
     * @return
     */
    public static String[] getSplit(String str,String split) {
        String mStr = str.replaceAll(split, "v^" + split + "v^");
        String[] strs = mStr.split(split);
        for (int i = 0; i < strs.length; i++) {
            strs[i] = strs[i].replaceAll("v\\^", "");
        }
        return strs;
    }

}
